package com.example.administrator.x3picturetransliteration.Home.Fragment;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.example.administrator.x3picturetransliteration.Base.BaseFragment;
import com.example.administrator.x3picturetransliteration.Base.BasePresenter;
import com.example.administrator.x3picturetransliteration.Bean.AppChannelBean;
import com.example.administrator.x3picturetransliteration.Bean.CarouselListBean;
import com.example.administrator.x3picturetransliteration.Constants;
import com.example.administrator.x3picturetransliteration.Home.Activity.AgreementActivity;
import com.example.administrator.x3picturetransliteration.Home.Activity.AppActivity;
import com.example.administrator.x3picturetransliteration.Home.Activity.BuyingMembersActivity;
import com.example.administrator.x3picturetransliteration.Home.Activity.RecognitionResultsActivity;
import com.example.administrator.x3picturetransliteration.Home.Activity.TranslateActivity;
import com.example.administrator.x3picturetransliteration.Home.Presenter.Fragment.NavigationPresenter;
import com.example.administrator.x3picturetransliteration.R;
import com.example.administrator.x3picturetransliteration.Util.Img.PicassoEqualProportionTransformation2;
import com.example.administrator.x3picturetransliteration.Util.MediaUtil;
import com.example.administrator.x3picturetransliteration.Util.ToastUtil;
import com.muzhi.camerasdk.model.CameraSdkParameterInfo;
import com.squareup.picasso.Picasso;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;


import java.io.File;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions;

/**
 * 识别
 * Created by Administrator on 2019/8/16.
 */

@RuntimePermissions
public class NavigationFragment extends BaseFragment{

    @BindView(R.id.TitleText)TextView TitleText;

    @BindView(R.id.banner)Banner banner;

    private NavigationPresenter mNavigationPresenter;

    @Override
    protected int intiLayout() {
        return R.layout.home_fragment_navigation;
    }

    @Override
    public void operation(View view) {

        mNavigationPresenter = new NavigationPresenter(getContext(),mCompositeSubscriptions);

        TitleText.setText("图片文字识别");

        banner.setImageLoader(new GlideImageLoader());

        mNavigationPresenter.carouselList(new BasePresenter.Callback() {
            @Override
            public void getData(Object data) {

                List<CarouselListBean> CarouselListData = (List<CarouselListBean>)data;

                banner.setImages(CarouselListData);
                banner.start();

            }
        });

    }

    @Override
    public void LazyLoad() {

    }

    /** 图片上传识别 */
    @OnClick(R.id.scsbView)
    public void scsbViewClick(){

//        if ("0".equals(Constants.getConstants().getmUserDataBean().getVipType())){
//
//            new ToastUtil(getContext()).getToast(false,"请先购买会员").show();
//            return;
//        }

        NavigationFragmentPermissionsDispatcher.getImgWithCheck(this,1,false,null);
    }

    /** 拍照识别 */
    @OnClick(R.id.pzsbView)
    public void pzsbViewClick(){

//        if ("0".equals(Constants.getConstants().getmUserDataBean().getVipType())){
//
//            new ToastUtil(getContext()).getToast(false,"请先购买会员").show();
//            return;
//        }

        ((AppActivity)getContext()).showCameraActionsts(new AppActivity.Camera() {
            @Override
            public void getImg(final CameraSdkParameterInfo mCameraSdkParameterInfo) {

                mNavigationPresenter.app_channel(new BasePresenter.Callback() {
                    @Override
                    public void getData(Object qdata) {

                        AppChannelBean mAppChannelBean = (AppChannelBean)qdata;

                        if (!"0".equals(Constants.getConstants().getmUserDataBean().getVipType()) || mAppChannelBean.getStatus()==0){

                            RecognitionResultsActivity.startRecognitionResultsActivity(NavigationFragment.this.getContext(), JSON.toJSONString(mCameraSdkParameterInfo.getImage_list()));

                        }else {

                            for (String file:mCameraSdkParameterInfo.getImage_list()){

                                File IFile = new File(file);
                                //IFile.delete();
                                MediaUtil.updateFileFromDatabase(NavigationFragment.this.getContext(),IFile);
                            }
                            new ToastUtil(NavigationFragment.this.getContext()).getToast(false,"请先购买VIP").show();
                            NavigationFragment.this.getContext().startActivity(new Intent(NavigationFragment.this.getContext(),BuyingMembersActivity.class));
                        }
                    }
                });

            }
        });
    }

    /** 批量图片识别 */
    @OnClick(R.id.tpView)
    public void tpViewClick(){

//        if ("0".equals(Constants.getConstants().getmUserDataBean().getVipType())){
//
//            new ToastUtil(getContext()).getToast(false,"请先购买会员").show();
//            return;
//        }

        NavigationFragmentPermissionsDispatcher.getImgWithCheck(this,9,false,null);
    }

    /** 表格图片识别 */
    @OnClick(R.id.bgsbView)
    public void bgsbViewClick(){

//        if ("0".equals(Constants.getConstants().getmUserDataBean().getVipType())){
//
//            new ToastUtil(getContext()).getToast(false,"请先购买会员").show();
//            return;
//        }

//        new ExamplesDialog(getContext(), "EXCECL示例", "图片扫描软件采用业界领先的人工智能OCR技术  可以识别文档上所有表格里面的信息内容，识别率超过99.7%\n" +
//                "请注意图片清晰无破损 大小不超过4M，最长边不超过4096像素！", new ExamplesDialog.hd() {
//            @Override
//            public void hdff() {
//
//                NavigationFragmentPermissionsDispatcher.getImgWithCheck(NavigationFragment.this,1,false,"表格识别");
//            }
//        }).show();

        NavigationFragmentPermissionsDispatcher.getImgWithCheck(NavigationFragment.this,1,false,"表格识别");
    }

    /** 手写识别 */
    @OnClick(R.id.sxsbView)
    public void sxsbViewClick(){

//        if ("0".equals(Constants.getConstants().getmUserDataBean().getVipType())){
//
//            new ToastUtil(getContext()).getToast(false,"请先购买会员").show();
//            return;
//        }

        NavigationFragmentPermissionsDispatcher.getImgWithCheck(this,1,false,"手写识别");
    }

    /** 上传图片翻译 */
    @OnClick(R.id.tpfyView)
    public void tpfyViewClick(){

//        if ("0".equals(Constants.getConstants().getmUserDataBean().getVipType())){
//
//            new ToastUtil(getContext()).getToast(false,"请先购买会员").show();
//            return;
//        }

        NavigationFragmentPermissionsDispatcher.getImgWithCheck(this,1,true,null);
    }

    /** 拍照翻译 */
    @OnClick(R.id.pzfyView)
    public void pzfyViewClick(){

//        if ("0".equals(Constants.getConstants().getmUserDataBean().getVipType())){
//
//            new ToastUtil(getContext()).getToast(false,"请先购买会员").show();
//            return;
//        }

        ((AppActivity)getContext()).showCameraActionsts(new AppActivity.Camera() {
            @Override
            public void getImg(final CameraSdkParameterInfo mCameraSdkParameterInfo) {

                mNavigationPresenter.document(mCameraSdkParameterInfo.getImage_list(), new BasePresenter.Callback() {
                    @Override
                    public void getData(Object data) {

                        Map<String,String> map = (Map<String,String>)data;

                        final String imgs = map.get("imgs");
                        final String result = map.get("result");

                        mNavigationPresenter.app_channel(new BasePresenter.Callback() {
                            @Override
                            public void getData(Object qdata) {

                                AppChannelBean mAppChannelBean = (AppChannelBean)qdata;

                                if (!"0".equals(Constants.getConstants().getmUserDataBean().getVipType()) || mAppChannelBean.getStatus()==0){

                                    TranslateActivity.startTranslateActivity(NavigationFragment.this.getContext(),imgs,result);

                                }else {

                                    File IFile = new File(imgs);
                                    //IFile.delete();
                                    MediaUtil.updateFileFromDatabase(NavigationFragment.this.getContext(),IFile);
                                    new ToastUtil(NavigationFragment.this.getContext()).getToast(false,"请先购买VIP").show();
                                    NavigationFragment.this.getContext().startActivity(new Intent(NavigationFragment.this.getContext(),BuyingMembersActivity.class));
                                }
                            }
                        });

                    }
                });
            }
        });
    }

    @OnClick(R.id.xhdqjsggView)
    public void cnmgb(){

//        new ExamplesDialog(getContext(), "WORD示例", "通过文档结构化识别与还原功能，将文档版面格式抽离并按顺序输出。\n" +
//                "请注意图片清晰无破损 大小不超过4M，最长边不超过4096像素！", new ExamplesDialog.hd() {
//            @Override
//            public void hdff() {
//
//                NavigationFragmentPermissionsDispatcher.getImgWithCheck(NavigationFragment.this,1,false,"文档结构化识别");
//            }
//        }).show();

        NavigationFragmentPermissionsDispatcher.getImgWithCheck(NavigationFragment.this,1,false,"文档结构化识别");
    }

    /** 轮播图适配器 */
    private class GlideImageLoader extends ImageLoader{

        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {

            final Map<String,String> map = (Map<String, String>)path;

            Picasso.with(NavigationFragment.this.getContext())
                    .load(map.get("img"))
                    .placeholder(R.drawable.home_advertisement)
                    .error(R.drawable.home_advertisement)
                    .transform(new PicassoEqualProportionTransformation2(imageView))
                    .into(imageView);

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

//                    AgreementActivity.startAgreementActivity(NavigationFragment.this.getContext(),"首页",map.get("url"));
//                    mNavigationPresenter.Statistics("2");
                }
            });
        }

    }

    /** 选择照片 */
    @NeedsPermission({Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA})
    public void getImg(final int max_image, final boolean Translate,String type){

        CameraSdkParameterInfo mCameraSdkParameterInfo = new CameraSdkParameterInfo();
        mCameraSdkParameterInfo.setShow_camera(true);
        mCameraSdkParameterInfo.setFilter_image(true);
        mCameraSdkParameterInfo.setMax_image(max_image);
        Intent intent = new Intent();
        intent.setClassName(getContext(), "com.muzhi.camerasdk.PhotoPickActivity");

        Bundle b = new Bundle();
        b.putSerializable(CameraSdkParameterInfo.EXTRA_PARAMETER, mCameraSdkParameterInfo);
        intent.putExtras(b);
        intent.putExtra("type",type);

        if (!Translate){


            ((Activity)getContext()).startActivityForResult(intent, CameraSdkParameterInfo.TAKE_PICTURE_FROM_GALLERY);
        }else {

            ((Activity)getContext()).startActivityForResult(intent, 2048);
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // NOTE: delegate the permission handling to generated method
        NavigationFragmentPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }
}
